package com.wo.android.pockybill.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wo.android.pockybill.R;

public class TitleView extends ViewGroup {

	private Context c;
	private RectF rect;

	public TitleView(Context Context, AttributeSet attrs) {
		super(Context, attrs);
		this.c = Context;

	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		LayoutInflater layoutInflater = (LayoutInflater) this.c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(R.layout.title_view, this);

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		float w = r - l;
		float h = b - t;
		rect = new RectF(w / 8, h / 8, 7 * w / 8, 7 * h / 8);
		float theta = (float) (2 * Math.PI / getChildCount());
		for (int i = 0; i < getChildCount(); i++) {
			View v = getChildAt(i);
			w = (rect.right - rect.left) / 2;
			h = (rect.bottom - rect.top) / 2;
			float half = Math.max(w, h) / 2;
			float centerX = rect.centerX() + w * FloatMath.cos(theta);
			float centerY = rect.centerY() + h * FloatMath.sin(theta);
			v.layout((int) (centerX - half), (int) (centerY - half),
					(int) (centerX + half), (int) (centerY + half));
		}

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		setMeasuredDimension(width, height);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

}
